/**
 * @file XYXKC
 * @brief XYXKC's ESPONE Mind+ library.
 * @n This is the MIND+expansion module for NVS small quantity data storage on the control board.
 *
 * @copyright    XYXKC,2023
 * @copyright    MIT Lesser General Public License
 *
 * @author [email](hyc@xyxedu.cn)
 * @date  2023-11-21
 */
enum TYPE{
    //% block="characterString"
    String,
    //% block="number"
    Float,


}

//% color="#ff97cd" iconWidth=50 iconHeight=40
namespace PREFERENCES {
    //% block="openNamespace[NAME]" blockType="command"
    //% NAME.shadow="String" NAME.defl="mynamespace"
    export function openNamespace(parameter: any, block: any) {
        let name = parameter.NAME.code;
        Generator.addInclude(`prefsInclude`,'#include <Preferences.h>')
        Generator.addObject(`prefsObject`,`Preferences `,'prefs;')
        Generator.addCode(`prefs.begin(${name});`)
    }

    //% block="getTheKeyNameInTheCurrentNamespaceAs[KEY]type[TYPE]valueOf" blockType="reporter"
    //% KEY.shadow="String" KEY.defl="key"
    //% TYPE.shadow="dropdown" TYPE.options="TYPE" TYPE.defl="TYPE.String"
    export function getValue (parameter: any, block: any) {
        let key = parameter.KEY.code;
        let type = parameter.TYPE.code
        let value = type == "String"? "\"\"":0
        Generator.addCode(`prefs.get${type}(${key},${value})`)
    }

    //% block="setTheKeyNameInTheCurrentNamespaceTo[KEY]theValueOfStringTypeIs[VALUE]" blockType="command"
    //% KEY.shadow="String" KEY.defl="key"
    //% VALUE.shadow="String" VALUE.defl="value"
    export function setStringValue (parameter: any, block: any) {
        let key = parameter.KEY.code;
        let value = parameter.VALUE.code
        Generator.addCode(`prefs.putString(${key},${value});`)
    }

    //% block="setTheKeyNameInTheCurrentNamespaceTo[KEY]theValueOfTheNumberTypeIs[VALUE]" blockType="command"
    //% KEY.shadow="String" KEY.defl="key"
    //% VALUE.shadow="number" VALUE.defl="1"
    export function setFloatValue (parameter: any, block: any) {
        let key = parameter.KEY.code;
        let value = parameter.VALUE.code
        Generator.addCode(`prefs.putFloat(${key},${value});`)
    }

    //% block="deleteTheKeyNameInTheCurrentNamespaceAs[KEY]elementsOf" blockType="command"
    //% KEY.shadow="String" KEY.defl="key"
    export function removeKey (parameter: any, block: any) {
        let key = parameter.KEY.code;
        Generator.addCode(`prefs.remove(${key});`)
    }
    //% block="deleteAllElementsInTheCurrentNamespace" blockType="command"
    export function removeWholeKey (parameter: any, block: any) {
        Generator.addCode(`prefs.clear();`)
    }
    //% block="obtainRemainingAvailableSpace" blockType="reporter"
    export function obtainRemainingAvailableSpace (parameter: any, block: any) {

        Generator.addCode(`prefs.freeEntries()`)
    }

    //% block="closeTheCurrentNamespace" blockType="command"
    export function closeTheCurrentNamespace(parameter: any, block: any) {
        Generator.addObject(`prefsObject`,`Preferences `,'prefs;')
        Generator.addCode(`prefs.end();`)
    }
}
